package com.ken.business.protocol.vaildhandler;

import com.ken.business.protocol.input.StudentInput;
import com.ken.common.web.vaild.annotation.KenVaild;
import com.ken.common.web.vaild.handler.KenVaildHandler;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;

@Component
public class BirthdayVaildHandler implements KenVaildHandler<StudentInput> {

    @Override
    public boolean vaild(KenVaild kenVaild, StudentInput data) {
        System.out.println("自定义的校验触发！");

        Integer age = data.getAge();
        Date birthday = data.getBirthday();
        if (age == null || birthday == null) return true;
        //当前时间
        Date now = new Date();
        //日历对象
        Calendar calender = Calendar.getInstance();
        calender.setTime(now);
        int nowYear = calender.get(Calendar.YEAR);
        calender.setTime(birthday);
        int birthdayYear = calender.get(Calendar.YEAR);

        if (nowYear - birthdayYear == age)
            return true;

        return false;
    }
}
